package com.qqtech.qquan.community.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.model.CommunityMessageQuery;
import com.qqtech.qquan.community.model.CommunityMessageVo;

@Repository
public class CommunityMessageDaoImpl extends BaseDaoImpl<CommunityMessage> implements CommunityMessageDao {
	public static final Logger log = LoggerFactory.getLogger(CommunityMessageDaoImpl.class);

	@Override
	public int updatePv(Integer id) {
		return sqlSessionTemplate.update(getSqlName("updateAddPv"), id);
	}
	@Override
	public int increaseComment(Integer id, int addCount) {
		CommunityMessage update = new CommunityMessage();
		update.setId(id);
		update.setCommentCount(addCount);
		return sqlSessionTemplate.update(getSqlName("increaseComment"), update);
	}
	
	@Override
	public int queryCountAdmin(CommunityMessageQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountAdmin"), BeanUtil.toMap(query));
	}
	
	@Override
	public Page<CommunityMessageVo> queryPageListAdmin(CommunityMessageQuery query,
			Pageable pageable) {
		List<CommunityMessageVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
		return new PageImpl<CommunityMessageVo>(contentList, pageable, this.queryCountAdmin(query));
	}

	@Override
	public int increasePraise(Integer id, int addCount) {
		CommunityMessage update = new CommunityMessage();
		update.setId(id);
		update.setPraiseCount(addCount);
		return sqlSessionTemplate.update(getSqlName("increasePraise"), update);
	}
	@Override
	public int queryCountFriendUp(CommunityMessageQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountFriendUp"), BeanUtil.toMap(query));
	}
	@Override
	public Page<CommunityMessageVo> queryPageFriendUp(CommunityMessageQuery query, Pageable pageable) {
		List<CommunityMessageVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectFriendUp"), getParams(query, pageable));
		return new PageImpl<CommunityMessageVo>(contentList, pageable, this.queryCountFriendUp(query));
	}

	@Override
	public CommunityMessageVo queryTotalCountByCreatorId(CommunityMessageQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountByCreatorId"), BeanUtil.toMap(query));
	}

	@Override
	public List<CommunityMessageVo> queryTotalCountByCreatorIds(CommunityMessageQuery query) {
		return sqlSessionTemplate.selectList(getSqlName("selectCountByCreatorIds"), BeanUtil.toMap(query));
	}
}
